import Config from './config.js';

export default {
	
	//接口请求
	request(params){
        return new Promise((resolve,reject)=>{
            //本地token
            let token = wx.getStorageSync('token');
            //登录接口不需要携带token
            if (params.api == '/user/token'){
                token = '';
            }
            //发起请求
            wx.request({
                    url: Config.baseRequestUrl+params.api,
                    method:params.method ? params.method.toUpperCase() : 'GET',
                    data: params.data ? params.data : {},
                    header: {
                    "app-id": 38,
                    "app-version": "1.0.0",
                    "channel":"mp",
                    "source": "wechat",
                    "token": token,
                },
                success: (resRawData) => {
                    let responseData = resRawData.data;
                    if(!responseData.code){
                        //正常返回
                        resolve({
                            status:'success',
                            data:responseData.data
                        });
                    }else{
                        //其他类型错误返回错误信息
                        resolve({
                            status:'error',
                            data:responseData.msg
                        });
                    }
                },
                fail:(err)=>{
                    //请求错误
                    reject({
                        status:'fail',
                        data:err.errMsg
                    }) 
                },
            });
        }).catch(err=>{
            wx.showToast({
                icon:'none',
                title: err.data
            })
            console.log('err',err)
        })
	}
}